package org.example.shop_front_api.controller;

import org.example.shop_front_api.entity.ApiAccount;
import org.example.shop_front_api.service.ApiAcccountService;
import org.example.shop_front_api.utils.JsonResult;
import org.example.shop_front_api.utils.JwtUtils;
import org.example.shop_front_api.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("api/api_account")
public class ApiAccountController {
    @Autowired
    private ApiAcccountService apiAcccountService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value("${salt}")
    private String salt;
    @PostMapping("register")
    public JsonResult register(@RequestBody ApiAccount account) {
        //验证参数
        String username = account.getUsername();
        String password = account.getPassword();
        if (username == null || password == null) {
            return JsonResult.failed("账号或密码不能为空");
        }
        //验证用户名不能重复
        ApiAccount accountInfo = apiAcccountService.getAccountByUsername(username);
        if (accountInfo != null) {
            return JsonResult.failed("该账号已经存在，请重新输入.");
        }
        //新增账号
        //md5密码加密
        account.setPassword(Md5Utils.encrypt(password));
        int result = apiAcccountService.insertAccount(account);
        if (result == 0) {
            return JsonResult.failed("注册失败.");
        }
        return JsonResult.success("注册成功", null);
    }

    //账号激活设置
    @GetMapping("set")
    public JsonResult set(@RequestParam("id") int id,
                          @RequestParam("enabled") int enabled) {
        int result = apiAcccountService.updateApiAccountEnabled(id, enabled);

        if (result == 0) {
            return JsonResult.failed("操作失败.");

        }
        return JsonResult.success("操作成功",null);

    }
    //登录
    @PostMapping("login")
    public JsonResult login(@RequestBody ApiAccount account) {
        //验证参数
        String username = account.getUsername();
        String password = account.getPassword();
        if (username == null || password == null) {
            return JsonResult.failed("账号或密码不能为空");
        }
        //根据用户名查询
        String safePassword = Md5Utils.encrypt(password);
        ApiAccount accountInfo = apiAcccountService.getAccountByUsername(username);
        if (accountInfo == null || !accountInfo.getPassword().equals(safePassword)) {
            return JsonResult.failed("账号或密码输入错误.");
        }
        if (accountInfo.getEnabled() == 0) {
            return JsonResult.failed("该账号不可用，请输入管理员.");

        }
        //获取token
        //String token = Md5Utils.encrypt(safePassword + salt);
        String jwtToken = JwtUtils.generateToken(username);
        //存储到redis
        stringRedisTemplate.opsForValue().set(jwtToken,jwtToken,3600*2, TimeUnit.SECONDS);
        return JsonResult.success("登录成功",jwtToken);
    }
}
